const { Blog,User } = require('./model')

!(async function(){
  // 查询一条记录
  const zhangsan = await User.findOne({
    where: {
      userName: 'zhangsan'
    }
  })
  console.log('zhangsan',zhangsan.dataValues)
  // 查询一条记录,只要值userName，nickName
  const zhangsanName = await User.findOne({
    attributes:['userName','nickName'],
    where: {
      userName: 'zhangsan'
    }
  })
  console.log('zhangsanName',zhangsanName.dataValues)
  // 查询列表
  const zhangsanBlogList = await Blog.findAll({
    where: {
      userId:1
    },
    order:[
      ['id','desc']
    ]
  })
  console.log(
    'zhangsanList',
    zhangsanBlogList.map(blog => blog.dataValues)
  )
  // 分页
  const blogPageList = await Blog.findAll({
    limit:1, // 当前页查询两条
    offset:0,// 跳过多少条到第二页
    order:[
      ['id','desc']
    ]
  })
  console.log(
    'blogPageList',
    blogPageList.map(blog => blog.dataValues)
  )
  // 查询总数
  const blogListAndCount = await Blog.findAndCountAll({
    limit:2, // 限制本次查询2条
    offset:0,
    order:[
      ['id','desc']
    ]
  })
  console.log(
    'blogListAndCount',
    blogListAndCount.count, // 总数，不考虑分页
    blogListAndCount.rows.map(blog => blog.dataValues)
  )
  // 连表查询1
  const blogListWithUser = await Blog.findAndCountAll({
    order:[
      ['id','desc']
    ],
    include: [
      {
        model:User,
        attributes:['nickName','userName'],
        where: {
          userName: 'zhangsan'
        }
      }
    ]
  })
  console.log(
    'blogListWithUser',
    blogListWithUser.count,
    blogListWithUser.rows.map(blog => {
      const blogVal = blog.dataValues
      blogVal.user = blogVal.user.dataValues
      return blogVal
    })
  )
  // 连表查询2 一个用户对应好多条博客
  const userListWithBlog = await User.findAndCountAll({
    attributes:['userName','nickName'],
    include:[
      {
        model:Blog
      }
    ]
  })
  console.log('userListWithBlog',
  userListWithBlog.count,
  userListWithBlog.rows.map(user=>{
    const userVal = user.dataValues
    userVal.blogs = userVal.blogs.map(blog => blog.dataValues)
    return userVal
  }))
})()